<?php
/**
 * Service of Metadata
 *
 * @author fusq
 */

require_once BaseUtil::getModulePath("base")  .  '/services/BaseService.php';
require_once BaseUtil::getModulePath('dbgrid') . '/DBGrid.php';
require_once BaseUtil::getModulePath("metadata")  .  '/models/MetadataDao.php';
require_once BaseUtil::getModulePath("metadata")  .  '/models/MetadataOptionDao.php';
require_once   BaseUtil::getModulePath("metadata")  .  '/constants/MetadataConstant.php';
require_once BaseUtil::getModulePath("systemlog")  .  '/services/SystemLogService.php';

class MetadataService extends BaseService {

    private $metadataDao;

    private $metadataOptionDao;

    public function __construct() {
        parent::__construct();
        $this->metadataDao = new MetadataDao();
        $this->metadataDao->setDb($this->db);
        $this->metadataOptionDao = new MetadataOptionDao();
        $this->metadataOptionDao->setDb($this->db);
    }

    public function getMetadatas($view, $post){
        $selectOptions = array("0"=>$this->_translate->translate('noLabel'),"1"=>$this->_translate->translate('yesLabel'));
        $dbGrid = new DBGrid($view);
        $dbGrid->setPost($post);
        $dbGrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "metadats-code", $this->_translate->translate("code"));
        $dbGrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "metadats-title", $this->_translate->translate("title"));
        $dbGrid->addOptionsFilter(DBGridConstant::FILTER_TYPE_SELECT, "metadats-input_type", $this->_translate->translate("inputType"), MetadataConstant::getInputTypeList());
        $dbGrid->addOptionsFilter(DBGridConstant::FILTER_TYPE_SELECT, "metadats-in_basic_search", $this->_translate->translate("inBasicSearch"), $selectOptions);
        $dbGrid->addOptionsFilter(DBGridConstant::FILTER_TYPE_SELECT, "metadats-show_in_product", $this->_translate->translate("showInProduct"), $selectOptions);
        $dbGrid->addFetchTable("metadatas", "*");
        $dbGrid->addValueFilter("metadatas-is_delete", "0");
        $dbGrid->addFetchColumn("code", $this->_translate->translate("code"),"edit");
        $dbGrid->addFetchColumn("title", $this->_translate->translate("title"));
        $dbGrid->addFetchColumn("long_title", $this->_translate->translate("longTitle"));
        $dbGrid->addFetchColumn("input_type", $this->_translate->translate("inputType"),"",MetadataConstant::getInputTypeList());
        $dbGrid->addFetchColumn("value_required", $this->_translate->translate("valueRequired"),"",$selectOptions);
        $dbGrid->addFetchColumn("in_basic_search", $this->_translate->translate("inBasicSearch"),"",$selectOptions);
        $dbGrid->addFetchColumn("show_in_product", $this->_translate->translate("showInProduct"),"",$selectOptions);
        //module action
        $dbGrid->addAAModuleAction("metadata","add",$this->_translate->translate("addBtn"),"add");
        $dbGrid->setPageTitle($this->_translate->translate("metadataList"));
        $dbGrid->setModule('/metadata/metadata');
        return $dbGrid->render();
    }

    public function saveMetadata($metadataForm,$post){
        $metadata = array();
        $optionValues = array();
        $optionTitles = array();
        $optionOrders = array();
        $metadata['code'] = $metadataForm->getValue("code");
        $metadata['title'] = $metadataForm->getValue("title");
        $metadata['long_title'] = $metadataForm->getValue("longTitle");
        $metadata['input_type'] = $metadataForm->getValue("inputType");
        $metadata['value_required'] = $metadataForm->getValue("valueRequired");
        $metadata['in_basic_search'] = $metadataForm->getValue("inBasicSearch");
        $metadata['in_advanced_search'] = $metadataForm->getValue("inAdvanceSearch");
        $metadata['show_in_product'] = $metadataForm->getValue("showInProduct");
        $metadata['sort_order'] = $metadataForm->getValue("sortOrder");
        if($metadata['input_type'] == 1 || $metadata['input_type'] == 2){
            $metadata['default_value'] = trim($post['defaultValue']) ;
        }else if($metadata['input_type'] == 3 || $metadata['input_type'] == 4){
            $optionValues = $post['optionValue'];
            $optionTitles = $post['optionTitle'];
            $optionOrders = $post['optionOrder'];
        }
        $metadataId = $this->metadataDao->insertMetadata($metadata);
        if(!empty($optionValues)){
            $i = 0;
            foreach($optionValues as $optionValue){
                $metadataOption = array();
                $metadataOption['metadata_id'] = $metadataId;
                $metadataOption['value'] = trim($optionValue);
                $metadataOption['title'] = trim($optionTitles[$i]);
                $metadataOption['sort_order'] = trim($optionOrders[$i]);
                $this->metadataOptionDao->insertMetadataOption($metadataOption);
                $i++;
            }
        }
        //insert log
        $log=new SystemLogService();
        $loginUser = BaseUtil::getLoginUser();
        $log->insertSystemLog('ADDMETA',$loginUser['uname'], $metadata); 
    }

    public function getMetadata($id){
        return $this->metadataDao->getMetadata($id);
    }

    public function getOptionsByMetadataId($metadataId){
        $options = $this->metadataOptionDao->getOptionsByMetadataId($metadataId);
        return $options;
    }

    public function updateMetadata($metadataForm,$post){
        $metadata = array();
        $optionValues = array();
        $optionTitles = array();
        $optionOrders = array();
        //update metadata
        $metadataId = $post['id'];
        $inputType = $metadataForm->getValue("inputTypeValue");
        $metadata['title'] = $metadataForm->getValue("title");
        $metadata['long_title'] = $metadataForm->getValue("longTitle");
        $metadata['value_required'] = $metadataForm->getValue("valueRequired");
        $metadata['in_basic_search'] = $metadataForm->getValue("inBasicSearch");
        $metadata['in_advanced_search'] = $metadataForm->getValue("inAdvanceSearch");
        $metadata['show_in_product'] = $metadataForm->getValue("showInProduct");
        $metadata['sort_order'] = $metadataForm->getValue("sortOrder");
        if($inputType == 1 || $inputType == 2){
            $metadata['default_value'] = trim($post['defaultValue']) ;
        }else if($inputType == 3 || $inputType == 4){
            $optionValues = $post['optionValue'];
            $optionTitles = $post['optionTitle'];
            $optionOrders = $post['optionOrder'];
        }
        $this->metadataDao->updateMetadata($metadata, $metadataId);
        //update metadata option
        //delete option
        $this->metadataOptionDao->deleteOption($metadataId);
        //insert new option
        if(!empty($optionValues)){
            $i = 0;
            foreach($optionValues as $optionValue){
                $metadataOption = array();
                $metadataOption['metadata_id'] = $metadataId;
                $metadataOption['value'] = trim($optionValue);
                $metadataOption['title'] = trim($optionTitles[$i]);
                $metadataOption['sort_order'] = trim($optionOrders[$i]);
                $this->metadataOptionDao->insertMetadataOption($metadataOption);
                $i++;
            }
        }
        //insert log
        $log=new SystemLogService();
        $loginUser = BaseUtil::getLoginUser();
        $log->insertSystemLog('EDITMETA',$loginUser['uname'], $metadata,$metadataId); 
    }

    public function deleteMetadata($id){
        //delete metadata option
        $metadataOption['is_delete'] = 1;
        $this->metadataOptionDao->updateByMetadataId($metadataOption, $id);
        //delete metadata
        $metadata['is_delete'] = 1;
        $this->metadataDao->updateMetadata($metadata, $id);
        //insert log
        $log=new SystemLogService();
        $loginUser = BaseUtil::getLoginUser();
        $log->insertSystemLog('DELMETA',$loginUser['uname'],"",$id);        
    }

}